/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package qclient;

import chat.ChatClient;
import chat.ChatMessage;
import fsktms.client.NetClient;
import fsktms.request.RequestManager;
import fsktms.dispatcher.Dispatcher;
import fsktms.request.IRequest;
import java.io.IOException;
import java.net.Inet4Address;
import java.net.UnknownHostException;
import javax.swing.JOptionPane;
import model.ClientModel;

/**
 *
 * @author salm
 */
public class QClient implements Runnable
{
    private NetClient netClient;
    private Dispatcher disptacher;
    private int getMessageInterval = 100;
    private RequestManager requestManager;
    private ChatClient chatclient;
    
    private QClient() {
    }
    
    public static QClient getInstance() {
        return inst;
    }
    private static QClient inst = new QClient();
    
    public void initialize(String bllHost, int port)  throws UnknownHostException, IOException
    {
        this.netClient = new NetClient(bllHost, port);
        this.disptacher = new Dispatcher(netClient);
        this.requestManager = new RequestManager(netClient);
        this.chatclient = new ChatClient();
        this.disptacher.getMessageListeners().put(IRequest.REQUEST_MESSAGE_MAGICKEY, requestManager);
        this.disptacher.getMessageListeners().put(ChatMessage.CHAT_MESSAGE_MAGICKEY, this.chatclient);
        
        this.chatclient.getMessageListeners().add(ClientModel.getInstance());
    }

    @Override
    public void run()
    {
        (new Thread(this.getNetClient())).start();
        
        this.disptacher.run();
        
        System.out.println("Client disconnected");
    }

    /**
     * @return the netClient
     */
    public NetClient getNetClient() {
        return netClient;
    }

    /**
     * @return the getMessageInterval
     */
    public int getGetMessageInterval() {
        return getMessageInterval;
    }

    /**
     * @param getMessageInterval the getMessageInterval to set
     */
    public void setGetMessageInterval(int getMessageInterval) {
        this.getMessageInterval = getMessageInterval;
    }

    /**
     * @return the requestManager
     */
    public RequestManager getRequestManager() {
        return requestManager;
    }
}
